package com.edu.stress.test.service;

import com.edu.stress.test.constant.Constant;
import com.edu.stress.test.util.Codec2;
import com.edu.stress.test.util.HttpUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import java.util.Calendar;
import java.util.Date;

public class DownService {

    private static final Logger logger = Logger.getLogger(DownService.class);
    private String sign;
    private Long timestamp;
    private String requestUrl;
    private String sessionId;
    private String requestBody;
    private String response;


    public DownService(String tempDeviceId, String tempSecretReg, String tempSessionId) {
        sessionId = tempSessionId;
        Date date = new Date();
        timestamp =date.getTime() / 1000;
        sign = Codec2.hexString(tempSecretReg, tempDeviceId + sessionId + timestamp);
        requestUrl = Constant.DOMAIN + "/" + tempDeviceId + "/down/report";
    }

    public boolean down() {

        boolean resStatus = false;

        requestBody = "{\"sessionId\":\"" + sessionId + "\",\"sign\":\"" + sign + "\",\"timestamp\":\"" + timestamp + "\",\"report\":[" +
                "{\"ecuPartNum\":\"per111222\",\"ecuSVer\":\"456456\",\"result\":1,\"reason\":\"\",\"beginTime\":\"2018-07-19 05:22:33" + "\",\"endTime\":\"" + "2018-07-19" + "\"}]}";

        try {
            response = HttpUtil.postJson(requestUrl, requestBody);

            if (response != null && !response.trim().equals("")) {
                JSONObject rsjson = JSONObject.fromObject(response);
                if (rsjson.getString("status").equals("1000") && rsjson.getString("msg").equals("成功")) {
                    resStatus = true;
                } else {
                    logger.error("下载status:" + rsjson.getString("status"));
                    logger.error("下载msg:" + rsjson.getString("msg"));
                }
            } else {
                logger.error("下载服务调用异常，返回值为空");
            }

        } catch (Exception e) {
            logger.error("下载服务调用未知异常");
            e.printStackTrace();
        }

        logger.info("下载URL:" + requestUrl);
        logger.info("下载请求:" + requestBody);
        logger.info("下载响应:" + response);

        return resStatus;
    }

    public static Logger getLogger() {
        return logger;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public String getRequestUrl() {
        return requestUrl;
    }

    public void setRequestUrl(String requestUrl) {
        this.requestUrl = requestUrl;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public String getRequestBody() {
        return requestBody;
    }

    public void setRequestBody(String requestBody) {
        this.requestBody = requestBody;
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }
}
